package com.geniusk.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.geniusk.model.mapper.ChatMsgMapper;
import com.geniusk.model.pojo.ChatMsg;
import com.geniusk.model.vo.MessageVO;
import com.geniusk.service.ChatMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@Service
public class ChatMsgServiceImpl implements ChatMsgService {

    private static final int TWO_MINUTES = 2;

    private static final int TEN_MINUTES = 10;

    private static final int ONE_DAY = 1;

    private static final int TWO_DAYS = 2;

    @Autowired
    private ChatMsgMapper chatMsgMapper;

    @Override
    public int selectUnreadMsgCountByUserId(Long userId) {
        return chatMsgMapper.selectUnreadMsgCountByUserId(userId);
    }

    @Override
    public List<MessageVO> selectMsgListByUserId(Long userId) {
        List<Long> userIdList = chatMsgMapper.selectExistChatMsgUserId(userId);
        List<MessageVO> chatMsgList = new ArrayList<>();
        for (Long existUserId : userIdList) {
            ChatMsg chatMsg = new ChatMsg();
            chatMsg.setSenderId(existUserId);
            chatMsg.setReceiverId(userId);
            MessageVO messageVO = chatMsgMapper.selectLatestChatMsg(chatMsg);
            String msgType = messageVO.getMsgType();
            switch (msgType) {
                case "2":
                    messageVO.setMsg("[表情]");
                    break;
                case "3":
                    messageVO.setMsg("[图片]");
                    break;
                case "4":
                    messageVO.setMsg("[语音]");
                    break;
                case "5":
                    messageVO.setMsg("[视频]");
                    break;
            }
            Date createTime = messageVO.getCreateTime();
            Date nowDate = new Date();
            long intervalMinuteTime = DateUtil.between(createTime, nowDate, DateUnit.MINUTE);
            long intervalDateTime = DateUtil.betweenDay(createTime, nowDate, true);
            if (intervalMinuteTime < TWO_MINUTES) {
                messageVO.setMsgTime(DateUtil.format(createTime, getDateFormat("HH:mm")));
            } else if (intervalDateTime < ONE_DAY) {
                messageVO.setMsgTime(DateUtil.format(createTime, getDateFormat("HH:mm")));
            } else if (intervalDateTime < TWO_DAYS) {
                messageVO.setMsgTime(DateUtil.format(createTime, getDateFormat("昨天 HH:mm")));
            } else {
                messageVO.setMsgTime(DateUtil.format(createTime, getDateFormat("yyyy-MM-dd")));
            }
            int count = chatMsgMapper.selectUnreadMsgCount(chatMsg);
            if(count > 0){
                messageVO.setCount(String.valueOf(count));
            }
            chatMsgList.add(messageVO);
        }
        chatMsgList.sort((t1, t2) -> t2.getCreateTime().compareTo(t1.getCreateTime()));
        return chatMsgList;
    }

    @Override
    public List<ChatMsg> selectChatMsgByPage(ChatMsg chatMsg) {
        List<ChatMsg> chatMsgList = chatMsgMapper.selectChatMsgByPage(chatMsg);
        Date nowDate = new Date();
        int size = chatMsgList.size();
        for (int i = 0; i < chatMsgList.size(); i++) {
            ChatMsg nowChatMsg = chatMsgList.get(i);
            Date createTime = nowChatMsg.getCreateTime();
            long intervalMinuteTime = DateUtil.between(createTime, nowDate, DateUnit.MINUTE);
            long intervalDateTime = DateUtil.betweenDay(createTime, nowDate, true);
            if (intervalMinuteTime < TWO_MINUTES) {
                if (i + 1 < size) {
                    ChatMsg nextChatMsg = chatMsgList.get(i + 1);
                    Date nextCreateTime = nextChatMsg.getCreateTime();
                    long nextIntervalTime = DateUtil.between(nextCreateTime, createTime, DateUnit.MINUTE);
                    if (nextIntervalTime > TWO_MINUTES) {
                        nowChatMsg.setMsgTimeFlag(true);
                        nowChatMsg.setMsgTime(DateUtil.format(nowChatMsg.getCreateTime(), getDateFormat("HH:mm")));
                    } else {
                        nowChatMsg.setMsgTimeFlag(false);
                        nowChatMsg.setMsgTime(DateUtil.format(nowChatMsg.getCreateTime(), getDateFormat("HH:mm")));
                    }
                } else {
                    nowChatMsg.setMsgTimeFlag(true);
                    nowChatMsg.setMsgTime(DateUtil.format(nowChatMsg.getCreateTime(), getDateFormat("HH:mm")));
                }
            }
            if (intervalDateTime < ONE_DAY) {
                if (i + 1 < chatMsgList.size()) {
                    ChatMsg nextChatMsg = chatMsgList.get(i + 1);
                    Date nextCreateTime = nextChatMsg.getCreateTime();
                    long nextIntervalTime = DateUtil.between(nextCreateTime, createTime, DateUnit.MINUTE);
                    if (nextIntervalTime > TWO_MINUTES) {
                        nowChatMsg.setMsgTimeFlag(true);
                        nowChatMsg.setMsgTime(DateUtil.format(nowChatMsg.getCreateTime(), getDateFormat("HH:mm")));
                    } else {
                        nowChatMsg.setMsgTimeFlag(false);
                        nowChatMsg.setMsgTime(DateUtil.format(nowChatMsg.getCreateTime(), getDateFormat("HH:mm")));
                    }
                } else {
                    nowChatMsg.setMsgTimeFlag(true);
                    nowChatMsg.setMsgTime(DateUtil.format(nowChatMsg.getCreateTime(), getDateFormat("HH:mm")));
                }
            } else if (intervalDateTime < TWO_DAYS) {
                if (i + 1 < chatMsgList.size()) {
                    ChatMsg nextChatMsg = chatMsgList.get(i + 1);
                    Date nextCreateTime = nextChatMsg.getCreateTime();
                    long nextIntervalTime = DateUtil.between(nextCreateTime, createTime, DateUnit.MINUTE);
                    if (nextIntervalTime > TEN_MINUTES) {
                        nowChatMsg.setMsgTimeFlag(true);
                        nowChatMsg.setMsgTime(DateUtil.format(nowChatMsg.getCreateTime(), getDateFormat("昨天 HH:mm")));
                    } else {
                        nowChatMsg.setMsgTimeFlag(false);
                        nowChatMsg.setMsgTime(DateUtil.format(nowChatMsg.getCreateTime(), getDateFormat("昨天 HH:mm")));
                    }
                } else {
                    nowChatMsg.setMsgTimeFlag(true);
                    nowChatMsg.setMsgTime(DateUtil.format(nowChatMsg.getCreateTime(), getDateFormat("昨天 HH:mm")));
                }
            } else {
                if (i + 1 < chatMsgList.size()) {
                    ChatMsg nextChatMsg = chatMsgList.get(i + 1);
                    Date nextCreateTime = nextChatMsg.getCreateTime();
                    long nextIntervalTime = DateUtil.between(nextCreateTime, createTime, DateUnit.MINUTE);
                    if (nextIntervalTime > TEN_MINUTES) {
                        nowChatMsg.setMsgTimeFlag(true);
                        nowChatMsg.setMsgTime(DateUtil.format(nowChatMsg.getCreateTime(), getDateFormat("yyyy-MM-dd HH:mm")));
                    } else {
                        nowChatMsg.setMsgTimeFlag(false);
                        nowChatMsg.setMsgTime(DateUtil.format(nowChatMsg.getCreateTime(), getDateFormat("yyyy-MM-dd HH:mm")));
                    }
                } else {
                    nowChatMsg.setMsgTimeFlag(true);
                    nowChatMsg.setMsgTime(DateUtil.format(nowChatMsg.getCreateTime(), getDateFormat("yyyy-MM-dd HH:mm")));
                }
            }
        }
        return chatMsgList;
    }

    @Override
    public void insertChatMsg(ChatMsg chatMsg) {
        chatMsgMapper.insertChatMsg(chatMsg);
    }

    @Override
    public int signChatMsgByIds(Long[] ids) {
        int count = 0;
        if (ids.length > 0) {
            count = chatMsgMapper.signChatMsgByIds(ids);
        }
        return count;
    }

    @Override
    public ChatMsg selectChatMsgById(Long msgId) {
        return chatMsgMapper.selectChatMsgById(msgId);
    }

    @Override
    public int sendDeleteByIds(Long[] ids) {
        int count = 0;
        if (ids.length > 0) {
            count = chatMsgMapper.sendDeleteByIds(ids);
        }
        return count;
    }

    @Override
    public int receiveDeleteByIds(Long[] ids) {
        int count = 0;
        if (ids.length > 0) {
            count = chatMsgMapper.receiveDeleteByIds(ids);
        }
        return count;
    }

    /**
     * 获取时间格式
     *
     * @param format 格式
     * @return 时间格式
     */
    private SimpleDateFormat getDateFormat(String format) {
        TimeZone cnTimeZone = TimeZone.getTimeZone("Asia/Shanghai");
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        dateFormat.setTimeZone(cnTimeZone);
        return dateFormat;
    }
}
